/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
/* Copyright (C) 2012 Zongyou Yao. All rights reserved.     */

#ifndef CHAN_INCLUDED
#define CHAN_INCLUDED

/**
 * It is a checked runtime error to pass a null chan_t to any chan function, or
 * to call any chan function before calling thread_init.
 */
typedef struct chan_t * chan_t;

/**
 * Creates, initializes, and returns a new channel.
 * Exception mem_failed can be raised.
 */
chan_t chan_new(void);

/**
 * Waits for a corresponding chan_receive, then copies up to size bytes from ptr
 * to the receiver, and returns the number copied. See chan_receive for checked
 * runtime error.
 * Exception thread_alerted can be raised.
 */
int chan_send(chan_t c, const void *ptr, int size);

/**
 * Waits for a corresponding chan_send, then copies up to size bytes from the
 * sender to ptr, and returns the number copied. It is a checked runtime error
 * for ptr == null or size < 0.
 * Exception thread_alerted can be raised.
 */
int chan_receive(chan_t c, void *ptr, int size);

#endif
